package com.ruoyi.web.controller.baodao.service;

import com.ruoyi.baodao.mapper.BDMapper;
import com.ruoyi.baodao.vo.AgeCount;
import com.ruoyi.baodao.vo.AmountCount;
import com.ruoyi.baodao.vo.GenderCount;
import com.ruoyi.baodao.vo.HourCount;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DataSourceType;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

@Slf4j
@Service
@DataSource(value = DataSourceType.SLAVE)
public class StatementService  {
    @Autowired
    BDMapper bdMapper;

    // 查询24小时分段客流
   public List<HourCount> queryHourCount(String shopId,String startTime,String endTime,String groupType) {

        return bdMapper.queryHourCount(shopId,startTime,endTime,groupType);
    }

    // 查询男女分类客流
    public List<GenderCount> queryGenderCount(String shopId,String startTime,String endTime) {
        return bdMapper.queryGenderCount(shopId,startTime,endTime);
    }

    // 年龄段分段客流
    public  List<AgeCount> queryAgeCount(String shopId,String startTime,String endTime) {
/*       List<AgeCount> ageCounts = bdMapper.queryAgeCount(shopId,startTime,endTime);
        for (AgeCount a :
        ageCounts) {
            if(a.getGender().equals("1")){

            }
            if(a.getGender().equals("2")){

            }
        }*/
        return bdMapper.queryAgeCount(shopId,startTime,endTime);
    }

    // 根据24小时和性别统计销售金额
    public HashMap<String, HashMap> queryAmountCountByGender(String shopId, String startTime, String endTime) {
        List<AmountCount> amountCounts = bdMapper.queryAmountCountByGender(shopId, startTime, endTime);
        if(amountCounts.size() == 0)
            return new HashMap<>();
        log.info(amountCounts.toString());
        HashMap<Integer, String> map1 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map1.put(i, "0");
        }

        HashMap<Integer, String> map2 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map2.put(i, "0");
        }


        // 无数据补0
        for (AmountCount amountCount :
                amountCounts) {
            log.info(amountCount.getGender());
          //  if (amountCount.getGender().equals("1")) {
            if ("1".equals(amountCount.getGender())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                // date.getHour();
                log.info("data.getHour" + date.getHour());
                map1.replace(date.getHour(), amountCount.getAmount());
          //  } else if (amountCount.getGender().equals("2")) {
            } else if ("2".equals(amountCount.getGender())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                // date.getHour();
                log.info("data.getHour" + date.getHour());
                map2.replace(date.getHour(), amountCount.getAmount());
            }
        }

        HashMap<String, HashMap> result = new HashMap<>();
        result.put("male", map1);
        result.put("female", map2);
        return result;
    }

    // 根据24小时和年龄范围统计销售金额
    public  HashMap<String, HashMap> queryAmountCountByAge(String shopId,String startTime,String endTime) {
       List<AmountCount> amountCounts = bdMapper.queryAmountCountByAge(shopId,startTime,endTime);
       if(amountCounts.size() == 0)
           return new HashMap<>();
        HashMap<Integer, String> map1 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map1.put(i, "0");
        }
        HashMap<Integer, String> map2 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map2.put(i, "0");
        }

        HashMap<Integer, String> map3 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map3.put(i, "0");
        }

        HashMap<Integer, String> map4 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map4.put(i, "0");
        }

        HashMap<Integer, String> map5 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map5.put(i, "0");
        }

        HashMap<Integer, String> map6 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map6.put(i, "0");
        }
        HashMap<Integer, String> map7 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map7.put(i, "0");
        }
        HashMap<Integer, String> map8 = new LinkedHashMap<>();
        for (int i = 0; i < 24; i++) {
            map8.put(i, "0");
        }

        for (AmountCount amountCount :
                amountCounts) {
          //  if (amountCount.getAgeRange().equals("1")) {
            if ("1".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map1.replace(date.getHour(), amountCount.getAmount());
            } else if ("2".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map2.replace(date.getHour(), amountCount.getAmount());
            } else if ("3".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map3.replace(date.getHour(), amountCount.getAmount());
            }else if("4".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map4.replace(date.getHour(), amountCount.getAmount());
            }else if("5".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map5.replace(date.getHour(), amountCount.getAmount());
            }else if ("6".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map6.replace(date.getHour(), amountCount.getAmount());
            }else if ("7".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map7.replace(date.getHour(), amountCount.getAmount());
            }else if ("8".equals(amountCount.getAgeRange())) {
                LocalDateTime date = LocalDateTime.parse(amountCount.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                map8.replace(date.getHour(), amountCount.getAmount());
            }
        }
        HashMap<String, HashMap> result = new HashMap<>();
        result.put("one", map1);
        result.put("two", map2);
        result.put("three", map3);
        result.put("four", map4);
        result.put("five", map5);
        result.put("six", map6);
        result.put("seven", map7);
        result.put("eight", map8);
        return result;
      //  return amountCounts;
    }


}
